package com.hdoit.apollo.logical.stock.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.druid.util.StringUtils;
import com.hdoit.apollo.logical.billing.model.Billing;
import com.hdoit.apollo.logical.billing.service.IBillingRecordService;
import com.hdoit.apollo.logical.billing.service.IBillingService;
import com.hdoit.apollo.logical.cargo.model.Collocation;
import com.hdoit.apollo.logical.cargo.service.ICollocationService;
import com.hdoit.apollo.logical.common.enums.billing.BillingRecordType;
import com.hdoit.apollo.logical.common.enums.billing.BillingState;
import com.hdoit.apollo.logical.common.enums.billing.BillingType;
import com.hdoit.apollo.logical.common.enums.outStock.OutStockModifyMark;
import com.hdoit.apollo.logical.common.enums.outStock.OutStockState;
import com.hdoit.apollo.logical.common.enums.outStock.OutStockType;
import com.hdoit.apollo.logical.stock.dao.IOutStockDao;
import com.hdoit.apollo.logical.stock.model.OutStock;
import com.hdoit.apollo.logical.stock.model.OutStockCollocation;
import com.hdoit.apollo.logical.stock.model.OutStockLogistics;
import com.hdoit.apollo.logical.stock.model.OutStockShow;
import com.hdoit.apollo.logical.stock.model.Stock;
import com.hdoit.apollo.logical.stock.model.StockCollocation;
import com.hdoit.apollo.logical.stock.service.IOutStockCollocationService;
import com.hdoit.apollo.logical.stock.service.IOutStockLogisticsService;
import com.hdoit.apollo.logical.stock.service.IOutStockService;
import com.hdoit.apollo.logical.stock.service.IOutStockShowService;
import com.hdoit.apollo.logical.stock.service.IStockCollocationService;
import com.hdoit.apollo.logical.stock.service.IStockService;
import com.hdoit.apollo.logical.viewmodel.stock.OutStockSearch;
import com.hdoit.apollo.logical.viewmodel.stock.SendOutGoodsInfo;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.core.common.utils.CommonUtils;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.data.dao.base.impl.SuperService;
import com.tennetcn.data.enums.ModelStatus;
import com.tennetcn.data.message.PagerModel;
/**
 * @author      humaoshu
 * @email       
 * @createtime  2018年04月02日 11:57:49
 * @comment
 */
@Component
public class OutStockServiceImpl extends SuperService<OutStock> implements IOutStockService{

	@Autowired
	private IOutStockShowService outStockShowService;
	@Autowired
	private IOutStockCollocationService outStockCollocationService;
	@Autowired
	private IStockService stockService;
	@Autowired
	private IStockCollocationService stockCollocationService;
	@Autowired
	private IOutStockDao outStockDao;
	@Autowired
	private IOutStockLogisticsService outStockLogisticsService;
	@Autowired
	private IBillingService billingService;
	@Autowired
	private IBillingRecordService billingRecordService;
	@Autowired
	private ICollocationService collocationService;
	
	//批量出库
	@Override
	public boolean applyOutStock(OutStock outStock, List<OutStockShow> outStockShows) {
		List<OutStockCollocation> outStockCollocations = new ArrayList<OutStockCollocation>();
		if(super.applyChange(outStock)){
			if(outStock.getModelStatus() == ModelStatus.add){
				for (OutStockShow outStockShow : outStockShows) {
					if (StringUtils.isEmpty(outStockShow.getShowId())) {
						outStockShow.setShowId(CommonUtils.getKeyId());
					}
					outStockShow.setDeleteMark(YesOrNoInteger.no);
					outStockShow.setOutStockId(outStock.getOutStockId());
					outStockShow.setModelStatus(ModelStatus.add);
					
					Stock stock=stockService.queryModelByKey(outStockShow.getCargoId());
					if( stock!= null){
						stock.setModelStatus(ModelStatus.update);
						
						stock.setStockNum(stock.getStockNum() - outStockShow.getOutStockShowNum());
						stockService.applyChange(stock);
					}
					List<StockCollocation> stockCollocations = new ArrayList<StockCollocation>();
					for (OutStockCollocation outStockCollocation : outStockShow.getOutStockCollocations()) {
						if (StringUtils.isEmpty(outStockCollocation.getOutStockCollocationId())) {
							outStockCollocation.setOutStockCollocationId(CommonUtils.getKeyId());
						}
						outStockCollocation.setDeleteMark(YesOrNoInteger.no);
						outStockCollocation.setModelStatus(ModelStatus.add);
						outStockCollocations.add(outStockCollocation);
						
						for(StockCollocation stockCollocation:stock.getStockCollocations()) {
							if(stockCollocation.getCollocationId().equals(outStockCollocation.getCollocationId())) {
								stockCollocation.setModelStatus(ModelStatus.update);
								stockCollocation.setCollocationNum(stockCollocation.getCollocationNum()-outStockCollocation.getCollocationNum());
								stockCollocations.add(stockCollocation);
							}
						}
					}
					stockCollocationService.applyChanges(stockCollocations);
				}

			}else{
				for (OutStockShow outStockShow : outStockShows) {
					outStockShow.setModelStatus(ModelStatus.update);
					for (OutStockCollocation outStockCollocation : outStockShow.getOutStockCollocations()) {
						outStockCollocation.setModelStatus(ModelStatus.update);
						outStockCollocations.add(outStockCollocation);
					}
				}

			}

			return outStockShowService.applyChanges(outStockShows)&&outStockCollocationService.applyChanges(outStockCollocations);
		}
		return false;
	}

	@Override
	public OutStock queryModelById(String key) {
		return outStockDao.queryModelById(key);
	}

	//单订单出库
	@Override
	public boolean applyOutStockFahuo(OutStock outStock, List<OutStockShow> outStockShows,
			OutStockLogistics outStockLogistics) {
		List<OutStockCollocation> outStockCollocations = new ArrayList<OutStockCollocation>();
		List<OutStockCollocation> outStockCollocationsYuan =new ArrayList<OutStockCollocation>();
		List<OutStockShow> outStockShowsYuan =new ArrayList<OutStockShow>();
		Billing billingYuan=billingService.getBillingByKey(outStock.getBillingId());
		if(super.applyChange(outStock)){
			if(outStock.getModelStatus() == ModelStatus.add){
				
				billingYuan.getOutStock().setOutStockDebt(billingYuan.getOutStock().getOutStockDebt()-outStock.getOutStockNum());
				if(billingYuan.getOutStock().getOutStockDebt()<=0) {
					billingYuan.setBillingState(BillingState.completed);
					billingYuan.setModelStatus(ModelStatus.update);
					billingService.applyChange(billingYuan);
				}
				billingYuan.getOutStock().setModelStatus(ModelStatus.update);
				super.applyChange(billingYuan.getOutStock());
				
				for (OutStockShow outStockShow : outStockShows) {
					outStockShow.setShowId(CommonUtils.getKeyId());
					outStockShow.setOutStockId(outStock.getOutStockId());
					outStockShow.setDeleteMark(YesOrNoInteger.no);
					outStockShow.setModelStatus(ModelStatus.add);
					
					for (OutStockShow outStockShowYuan : billingYuan.getOutStock().getOutStockShows() ) {
						if(outStockShow.getShowIdYuan().equals(outStockShowYuan.getShowId())) {
							outStockShowYuan.setOutStockShowDebt(outStockShowYuan.getOutStockShowDebt()-outStockShow.getOutStockShowNum());
							outStockShowYuan.setModelStatus(ModelStatus.update);
							outStockShowsYuan.add(outStockShowYuan);
						}
					}
					
					Stock stock=stockService.queryModelByKey(outStockShow.getCargoId());

					List<StockCollocation> stockCollocations = new ArrayList<StockCollocation>();
					for (OutStockCollocation outStockCollocation : outStockShow.getOutStockCollocations()) {
						outStockCollocation.setOutStockCollocationId(CommonUtils.getKeyId());
						outStockCollocation.setShowId(outStockShow.getShowId());
						outStockCollocation.setDeleteMark(YesOrNoInteger.no);
						outStockCollocation.setModelStatus(ModelStatus.add);
						outStockCollocations.add(outStockCollocation);
						
						for(StockCollocation stockCollocation:stock.getStockCollocations()) {
							if(stockCollocation.getCollocationId().equals(outStockCollocation.getCollocationId())) {
								stockCollocation.setModelStatus(ModelStatus.update);
								if(outStockCollocation.getRatio()!=null&&outStockCollocation.getRatio()!=0) {
									stockCollocation.setCollocationNum(stockCollocation.getCollocationNum()-outStockCollocation.getCollocationNum()*outStockCollocation.getRatio());
									stock.setStockNum(stock.getStockNum() - outStockCollocation.getCollocationNum()*outStockCollocation.getRatio());
								}else {
									stockCollocation.setCollocationNum(stockCollocation.getCollocationNum()-outStockCollocation.getCollocationNum());
									stock.setStockNum(stock.getStockNum() - outStockCollocation.getCollocationNum());
								}
								stockCollocations.add(stockCollocation);
							}
						}

						for (OutStockShow outStockShowYuan : billingYuan.getOutStock().getOutStockShows() ) {
							for (OutStockCollocation outStockCollocationYuan : outStockShowYuan.getOutStockCollocations()) {
								if(outStockCollocation.getOutStockCollocationIdYuan().equals(outStockCollocationYuan.getOutStockCollocationId())) {
									
									outStockCollocationYuan.setCollocationDebt(outStockCollocationYuan.getCollocationDebt()-outStockCollocation.getCollocationNum());
									outStockCollocationYuan.setModelStatus(ModelStatus.update);
									outStockCollocationsYuan.add(outStockCollocationYuan);
								}
							}
						}
					}
					if( stock!= null){
						stock.setModelStatus(ModelStatus.update);
						stockService.applyChange(stock);
					}
					stockCollocationService.applyChanges(stockCollocations);
				}

			}else{
				for (OutStockShow outStockShow : outStockShows) {
					outStockShow.setModelStatus(ModelStatus.update);
					for (OutStockCollocation outStockCollocation : outStockShow.getOutStockCollocations()) {
						outStockCollocation.setModelStatus(ModelStatus.update);
						outStockCollocations.add(outStockCollocation);
					}
				}

			}
			billingRecordService.saveBillingRecord(outStock.getBillingId(),outStock.getOutStockNum(),0.0,BillingRecordType.fahuo,
													outStock.getOutStockId(),outStock.getShopId(),outStock.getCreateUserId(),outStock.getCreateUserName());
			
			saveOutStockLogistics(outStockLogistics,outStock.getOutStockId());
			outStockShowService.applyChanges(outStockShowsYuan);
			outStockCollocationService.applyChanges(outStockCollocationsYuan);
			return outStockShowService.applyChanges(outStockShows)&&outStockCollocationService.applyChanges(outStockCollocations);
		}
		return false;
	}

	//ionic上对同一个客户的多个订单一起发货的保存
	@Override
	public boolean applyOutStockFahuoList(List<Billing> billings, OutStockLogistics outStockLogistics, String loginId,
			String loginName) {
		List<OutStockCollocation> outStockCollocations = new ArrayList<OutStockCollocation>();
		List<OutStockCollocation> outStockCollocationsYuan =new ArrayList<OutStockCollocation>();
		List<OutStockShow> outStockShows =new ArrayList<OutStockShow>();
		List<OutStockShow> outStockShowsYuan =new ArrayList<OutStockShow>();
		if(billings!=null) {
			for(Billing billing:billings) {
				billing.getOutStock().setCreateDate(DateUtils.getCurrent());
				billing.getOutStock().setCreateUserId(loginId);
				billing.getOutStock().setCreateUserName(loginName);
				billing.getOutStock().setDeleteMark(YesOrNoInteger.no);
				billing.getOutStock().setType(OutStockType.fahuo);
				billing.getOutStock().setOutStockState(OutStockState.yichuku);
				
				Billing billingYuan=billingService.getBillingByKey(billing.getOutStock().getBillingId());
				if(super.applyChange(billing.getOutStock())){
					if(billing.getOutStock().getModelStatus() == ModelStatus.add){
						
						billingYuan.getOutStock().setOutStockDebt(billingYuan.getOutStock().getOutStockDebt()-billing.getOutStock().getOutStockNum());
						if(billingYuan.getOutStock().getOutStockDebt()<=0) {
							billingYuan.setBillingState(BillingState.completed);
							billingYuan.setModelStatus(ModelStatus.update);
							billingService.applyChange(billingYuan);
						}
						billingYuan.getOutStock().setModelStatus(ModelStatus.update);
						super.applyChange(billingYuan.getOutStock());
						
						for (OutStockShow outStockShow : billing.getOutStock().getOutStockShows()) {
							outStockShow.setShowId(CommonUtils.getKeyId());
							outStockShow.setOutStockId(billing.getOutStock().getOutStockId());
							outStockShow.setModelStatus(ModelStatus.add);
							outStockShow.setDeleteMark(YesOrNoInteger.no);
							outStockShows.add(outStockShow);
							for (OutStockShow outStockShowYuan : billingYuan.getOutStock().getOutStockShows() ) {
								if(outStockShow.getShowIdYuan().equals(outStockShowYuan.getShowId())) {
									outStockShowYuan.setOutStockShowDebt(outStockShowYuan.getOutStockShowDebt()-outStockShow.getOutStockShowNum());
									outStockShowYuan.setModelStatus(ModelStatus.update);
									outStockShowsYuan.add(outStockShowYuan);
								}
							}
							
							Stock stock=stockService.queryModelByKey(outStockShow.getCargoId());

							List<StockCollocation> stockCollocations = new ArrayList<StockCollocation>();
							for (OutStockCollocation outStockCollocation : outStockShow.getOutStockCollocations()) {
								outStockCollocation.setOutStockCollocationId(CommonUtils.getKeyId());
								outStockCollocation.setShowId(outStockShow.getShowId());
								outStockCollocation.setDeleteMark(YesOrNoInteger.no);
								outStockCollocation.setModelStatus(ModelStatus.add);
								outStockCollocations.add(outStockCollocation);
								
								for(StockCollocation stockCollocation:stock.getStockCollocations()) {
									if(stockCollocation.getCollocationId().equals(outStockCollocation.getCollocationId())) {
										stockCollocation.setModelStatus(ModelStatus.update);
										if(outStockCollocation.getRatio()!=null&&outStockCollocation.getRatio()!=0) {
											stockCollocation.setCollocationNum(stockCollocation.getCollocationNum()-outStockCollocation.getCollocationNum()*outStockCollocation.getRatio());
											stock.setStockNum(stock.getStockNum() - outStockCollocation.getCollocationNum()*outStockCollocation.getRatio());
										}else {
											stockCollocation.setCollocationNum(stockCollocation.getCollocationNum()-outStockCollocation.getCollocationNum());
											stock.setStockNum(stock.getStockNum() - outStockCollocation.getCollocationNum());
										}
										stockCollocations.add(stockCollocation);
									}
								}
								
								for (OutStockShow outStockShowYuan : billingYuan.getOutStock().getOutStockShows() ) {
									for (OutStockCollocation outStockCollocationYuan : outStockShowYuan.getOutStockCollocations()) {
										if(outStockCollocation.getOutStockCollocationIdYuan().equals(outStockCollocationYuan.getOutStockCollocationId())) {
											outStockCollocationYuan.setCollocationDebt(outStockCollocationYuan.getCollocationDebt()-outStockCollocation.getCollocationNum());
											outStockCollocationYuan.setModelStatus(ModelStatus.update);
											outStockCollocationsYuan.add(outStockCollocationYuan);
										}
									}
								}
							}
							if( stock!= null){
								stock.setModelStatus(ModelStatus.update);
								
								stockService.applyChange(stock);
							}
							stockCollocationService.applyChanges(stockCollocations);
						}

					}else{
						for (OutStockShow outStockShow : billing.getOutStock().getOutStockShows()) {
							outStockShow.setModelStatus(ModelStatus.update);
							outStockShows.add(outStockShow);
							for (OutStockCollocation outStockCollocation : outStockShow.getOutStockCollocations()) {
								outStockCollocation.setModelStatus(ModelStatus.update);
								outStockCollocations.add(outStockCollocation);
							}
						}

					}
					billingRecordService.saveBillingRecord(billing.getOutStock().getBillingId(),billing.getOutStock().getOutStockNum(),0.0,BillingRecordType.fahuo,
							billing.getOutStock().getOutStockId(),billing.getOutStock().getShopId(),billing.getOutStock().getCreateUserId(),billing.getOutStock().getCreateUserName());
					saveOutStockLogistics(outStockLogistics,billing.getOutStock().getOutStockId());
				}
			}
			outStockShowService.applyChanges(outStockShowsYuan);
			outStockCollocationService.applyChanges(outStockCollocationsYuan);
			return outStockShowService.applyChanges(outStockShows)&&outStockCollocationService.applyChanges(outStockCollocations);
		}
		return false;
	}

	private boolean saveOutStockLogistics(OutStockLogistics outStockLogistics,String outStockId) {
		outStockLogistics.setLogisticsId(CommonUtils.getKeyId());
		outStockLogistics.setOutStockId(outStockId);
		outStockLogistics.setDeleteMark(YesOrNoInteger.no);
		outStockLogistics.setModelStatus(ModelStatus.add);
		return outStockLogisticsService.applyChange(outStockLogistics);
	}

	@Override
	public boolean deleteOsutStockByKey(String outStockId) {
		return outStockDao.deleteOsutStockByKey(outStockId);
	}
	
	//关闭出库单
	@Override
	public boolean closeOutStock(OutStock outStock) {
		List<OutStockCollocation> outStockCollocationsYuan =new ArrayList<OutStockCollocation>();
		List<OutStockShow> outStockShowsYuan =new ArrayList<OutStockShow>();
		
		
		if(super.applyChange(outStock)) {
			Billing billingYuan=billingService.getBillingByKey(outStock.getBillingId());
			billingYuan.getOutStock().setOutStockDebt(billingYuan.getOutStock().getOutStockDebt()+outStock.getOutStockNum());
			if(BillingState.completed.equals(billingYuan.getBillingState())) {
				billingYuan.setBillingState(BillingState.notShipped);
				billingYuan.setModelStatus(ModelStatus.update);
				billingService.applyChange(billingYuan);
			}
			billingYuan.getOutStock().setModelStatus(ModelStatus.update);
			super.applyChange(billingYuan.getOutStock());
			
			outStock.setModelStatus(ModelStatus.update);
			outStock.setBillingType(BillingType.close);
			for (OutStockShow outStockShow : outStock.getOutStockShows()) {
				List<StockCollocation> stockCollocations = new ArrayList<StockCollocation>();
				Stock stock=stockService.queryModelByKey(outStockShow.getCargoId());
				if( stock!= null){
					stock.setModelStatus(ModelStatus.update);
					stock.setStockNum(stock.getStockNum() + outStockShow.getOutStockShowNum());
					stockService.applyChange(stock);
				}
				
				for (OutStockCollocation outStockCollocation : outStockShow.getOutStockCollocations()) {
					for(StockCollocation stockCollocation:stock.getStockCollocations()) {
						if(stockCollocation.getCollocationId().equals(outStockCollocation.getCollocationId())) {
							stockCollocation.setModelStatus(ModelStatus.update);
							stockCollocation.setCollocationNum(stockCollocation.getCollocationNum()+outStockCollocation.getCollocationNum()*outStockCollocation.getRatio());
							stockCollocations.add(stockCollocation);
						}
					}
				}
				stockCollocationService.applyChanges(stockCollocations);
				
				for (OutStockShow outStockShowYuan : billingYuan.getOutStock().getOutStockShows() ) {
					if(outStockShow.getShowIdYuan().equals(outStockShowYuan.getShowId())) {
						outStockShowYuan.setOutStockShowDebt(outStockShowYuan.getOutStockShowDebt() + outStockShow.getOutStockShowNum());
						outStockShowYuan.setModelStatus(ModelStatus.update);
						outStockShowsYuan.add(outStockShowYuan);
					}
				}
				for (OutStockCollocation outStockCollocation : outStockShow.getOutStockCollocations()) {
					for (OutStockShow outStockShowYuan : billingYuan.getOutStock().getOutStockShows() ) {
						for (OutStockCollocation outStockCollocationYuan : outStockShowYuan.getOutStockCollocations()) {
							if(outStockCollocation.getOutStockCollocationIdYuan().equals(outStockCollocationYuan.getOutStockCollocationId())) {
								outStockCollocationYuan.setCollocationDebt(outStockCollocationYuan.getCollocationDebt() + outStockCollocation.getCollocationNum());
								outStockCollocationYuan.setModelStatus(ModelStatus.update);
								outStockCollocationsYuan.add(outStockCollocationYuan);
							}
						}
					}
				}
				
	
			}
			billingRecordService.saveBillingRecord(outStock.getBillingId(),outStock.getOutStockNum(),0.0,BillingRecordType.sendCargoClose,
					outStock.getOutStockId(),outStock.getShopId(),outStock.getCreateUserId(),outStock.getCreateUserName());
			
			return outStockShowService.applyChanges(outStockShowsYuan)&&outStockCollocationService.applyChanges(outStockCollocationsYuan);
		}
		return false;
	}

	//PC端出库保存
	@Override
	public boolean applySendOutGoods(Billing billing, SendOutGoodsInfo sendOutGoodsInfo) {
		List<OutStockCollocation> outStockCollocationsYuan =new ArrayList<OutStockCollocation>();
		List<StockCollocation> stockCollocations = new ArrayList<StockCollocation>();
		List<OutStockShow> outStockShowsYuan =new ArrayList<OutStockShow>();
		if(StringUtils.isEmpty(sendOutGoodsInfo.getCollocationNum())){
			return true;
		}else {
			String[] cargoIds=sendOutGoodsInfo.getCargoId().split(",");
			String[] collocationIds=sendOutGoodsInfo.getCollocationId().split(",");
			String[] realPrices=sendOutGoodsInfo.getRealPrice().split(",");
			String[] collocationNums=sendOutGoodsInfo.getCollocationNum().split(",");
			String[] units=sendOutGoodsInfo.getUnitForTransform().split(",");
			
			String[] showIdYuans=sendOutGoodsInfo.getShowIdYuan().split(",");
			String[] outStockCollocationIdYuans=sendOutGoodsInfo.getOutStockCollocationIdYuan().split(",");
			
			Billing billingYuan=billingService.getBillingByKey(billing.getBillingId());
			
			
			OutStock outStock=new OutStock();
			outStock.setOutStockId(CommonUtils.getKeyId());
			outStock.setType(OutStockType.fahuo);
			outStock.setCreateDate(billing.getModifyDate());
			outStock.setCreateUserId(billing.getModifyUserId());
			outStock.setCreateUserName(billing.getModifyUserName());
			
			outStock.setShopId(billing.getShopId());
			outStock.setOutStockState(OutStockState.yichuku);
			outStock.setBillingId(billing.getBillingId());
			outStock.setDeleteMark(YesOrNoInteger.no);
			outStock.setModelStatus(ModelStatus.add);
			
			List<OutStockShow> outStockShows=new ArrayList<OutStockShow>();
			
			List<OutStockCollocation> outStockCollocations=new ArrayList<OutStockCollocation>();
			for(int i=0;i<collocationNums.length;i++){
				if(Double.parseDouble(collocationNums[i])>0) {
					OutStockCollocation outStockCollocation=new OutStockCollocation();
					Collocation collocation=collocationService.queryModelByCargo(collocationIds[i]);
					Double realPrice=Double.parseDouble(realPrices[i]);
					Double collocationNum=Double.parseDouble(collocationNums[i]);
					
					Boolean isCreateOutStockShow=true;
				
					if(isCreateOutStockShow) {
						OutStockShow outStockShow=new OutStockShow();
						outStockShow.setShowId(CommonUtils.getKeyId());
						outStockShow.setOutStockId(outStock.getOutStockId());
						outStockShow.setOutStockShowDebt(0.0);
						outStockShow.setReturnCargoNumShow(0.0);
						outStockShow.setDeleteMark(YesOrNoInteger.no);
						outStockShow.setCargoId(cargoIds[i]);
						outStockShow.setShowIdYuan(showIdYuans[i]);
						outStockShow.setCreateDate(billing.getModifyDate());
						outStockShow.setCreateUserId(billing.getModifyUserId());
						outStockShow.setCreateUserName(billing.getModifyUserName());
						outStockShow.setModelStatus(ModelStatus.add);
						//单位换算
						if (units[i].equals(collocation.getCargo().getUnit())) {
							outStockShow.setShowPrice(collocationNum*realPrice);
							outStockShow.setOutStockShowNum(collocationNum);
						}else if (units[i].equals(collocation.getCargo().getViceFirstUnit())) {
							outStockShow.setShowPrice(collocationNum*realPrice*collocation.getCargo().getFirstRatio());
							outStockShow.setOutStockShowNum(collocationNum*collocation.getCargo().getFirstRatio());
						}else if (units[i].equals(collocation.getCargo().getViceSecondUnit())) {
							outStockShow.setShowPrice(collocationNum*realPrice*collocation.getCargo().getSecondRatio());
							outStockShow.setOutStockShowNum(collocationNum*collocation.getCargo().getSecondRatio());
						}
						//
						
						
						outStockShows.add(outStockShow);
						outStockCollocation.setShowId(outStockShow.getShowId());
					}
					outStockCollocation.setOutStockCollocationId(CommonUtils.getKeyId());
					outStockCollocation.setCollocationId(collocationIds[i]);
					outStockCollocation.setCollocationNum(collocationNum);
					outStockCollocation.setCollocationDebt(0.0);
					outStockCollocation.setSendCargoNum(0.0);
					outStockCollocation.setReturnCargoNumCollocation(0.0);
					outStockCollocation.setRealPrice(realPrice);
					outStockCollocation.setUnit(units[i]);
					outStockCollocation.setOutStockCollocationIdYuan(outStockCollocationIdYuans[i]);
					//存入单位换算比率，方便库存操作
					if (units[i].equals(collocation.getCargo().getUnit())) {
						outStockCollocation.setRatio(1.0);
					}else if (units[i].equals(collocation.getCargo().getViceFirstUnit())) {
						outStockCollocation.setRatio(collocation.getCargo().getFirstRatio());
					}else if (units[i].equals(collocation.getCargo().getViceSecondUnit())) {
						outStockCollocation.setRatio(collocation.getCargo().getSecondRatio());
					}
					//
					/*outStockCollocation.setComment(comments[i]);*/
					outStockCollocation.setDeleteMark(YesOrNoInteger.no);
					outStockCollocation.setCreateDate(billing.getModifyDate());
					outStockCollocation.setCreateUserId(billing.getModifyUserId());
					outStockCollocation.setCreateUserName(billing.getModifyUserName());
					outStockCollocation.setModelStatus(ModelStatus.add);
					outStockCollocations.add(outStockCollocation);
					
					for (OutStockShow outStockShowYuan : billingYuan.getOutStock().getOutStockShows() ) {
						for (OutStockCollocation outStockCollocationYuan : outStockShowYuan.getOutStockCollocations()) {
							if(outStockCollocation.getOutStockCollocationIdYuan().equals(outStockCollocationYuan.getOutStockCollocationId())) {
								outStockCollocationYuan.setCollocationDebt(outStockCollocationYuan.getCollocationDebt()-outStockCollocation.getCollocationNum());
								outStockCollocationYuan.setModelStatus(ModelStatus.update);
								outStockCollocationsYuan.add(outStockCollocationYuan);
							}
						}
					}
				}
				
			}
			Double outStockNumAll=0.0;
			Stock stock=null;
			for(OutStockShow outStockShow:outStockShows) {
				outStockNumAll+=outStockShow.getOutStockShowNum();
				for (OutStockShow outStockShowYuan : billingYuan.getOutStock().getOutStockShows() ) {
					if(outStockShow.getShowIdYuan().equals(outStockShowYuan.getShowId())) {
						outStockShowYuan.setOutStockShowDebt(outStockShowYuan.getOutStockShowDebt()-outStockShow.getOutStockShowNum());
						outStockShowYuan.setModelStatus(ModelStatus.update);
						outStockShowsYuan.add(outStockShowYuan);
					}
				}
				
				stock=stockService.queryModelByKey(outStockShow.getCargoId());
				if( stock!= null){
					stock.setModelStatus(ModelStatus.update);
					
					stock.setStockNum(stock.getStockNum() - outStockShow.getOutStockShowNum());
					stockService.applyChange(stock);
				}
				
				for (OutStockCollocation outStockCollocation : outStockCollocations) {
					for(StockCollocation stockCollocation:stock.getStockCollocations()) {
						if(stockCollocation.getCollocationId().equals(outStockCollocation.getCollocationId())) {
							stockCollocation.setModelStatus(ModelStatus.update);
							stockCollocation.setCollocationNum(stockCollocation.getCollocationNum()-outStockCollocation.getCollocationNum()*outStockCollocation.getRatio());
							boolean allowAdd=true;
							for(StockCollocation stockCollocationtemp:stockCollocations) {
								if(stockCollocationtemp.getStockCollocationId().equals(stockCollocation.getStockCollocationId())) {
									allowAdd=false;
								}
							}
							if(allowAdd) {
								stockCollocations.add(stockCollocation);
							}
							
						}
					}
				}
				
			}

			outStock.setOutStockNum(outStockNumAll);
			outStock.setOutStockDebt(0.0);
			outStock.setOutStockTypeNum(outStockShows.size());
			outStock.setReturnCargoNum(0.0);
			
			
			billingYuan.getOutStock().setOutStockDebt(billingYuan.getOutStock().getOutStockDebt()-outStockNumAll);
			if(billingYuan.getOutStock().getOutStockDebt()<=0) {
				billingYuan.setBillingState(BillingState.completed);
				billingYuan.setModelStatus(ModelStatus.update);
				billingService.applyChange(billingYuan);
			}
			billingYuan.getOutStock().setModelStatus(ModelStatus.update);
			super.applyChange(billingYuan.getOutStock());
			billingRecordService.saveBillingRecord(billingYuan.getOutStock().getBillingId(),outStock.getOutStockNum(),0.0,BillingRecordType.fahuo,
					outStock.getOutStockId(),billingYuan.getOutStock().getShopId(),outStock.getCreateUserId(),outStock.getCreateUserName());
			
			stockCollocationService.applyChanges(stockCollocations);
			outStockShowService.applyChanges(outStockShowsYuan);
			outStockCollocationService.applyChanges(outStockCollocationsYuan);
			if(super.applyChange(outStock)) {
				return outStockShowService.applyChanges(outStockShows)&&outStockCollocationService.applyChanges(outStockCollocations);
			}
		}
		return false;
	}

	@Override
	public List<OutStock> queryListBySearch(OutStockSearch search) {
		return outStockDao.queryListBySearch(search);
	}

	@Override
	public boolean applyReturnPurchaseForPC(OutStock outStock) {
		
		List<OutStockCollocation> outStockCollocations = new ArrayList<OutStockCollocation>();
		List<OutStockShow> outStockShows=new ArrayList<OutStockShow>();
		List<OutStockShow> outStockShowsForAdd=new ArrayList<OutStockShow>();
		
		if(super.applyChange(outStock)){
			if(outStock.getModelStatus() == ModelStatus.add){
				String [] collocationIds=outStock.getCollocationIds().split(",");
				String [] numStrings=outStock.getOutStockNums().split(",");
				String [] prices=outStock.getOutStockPrices().split(",");
				
				
				for (int i = 0; i < collocationIds.length; i++) {
					
					Collocation collocation=collocationService.queryModel(collocationIds[i]);
					
					
					OutStockShow outStockShow=new OutStockShow();
					if (StringUtils.isEmpty(outStockShow.getShowId())) {
						outStockShow.setShowId(CommonUtils.getKeyId());
					}
					outStockShow.setCargoId(collocation.getCargoId());
					outStockShow.setOutStockId(outStock.getOutStockId());
					outStockShow.setShopId(outStock.getShopId());
					
					outStockShow.setOutStockShowNum(Double.parseDouble(numStrings[i]));
					outStockShow.setShowPrice(Integer.parseInt(numStrings[i])*Double.parseDouble(prices[i]));
					
					outStockShow.setCreateDate(DateUtils.getCurrent());
					outStockShow.setDeleteMark(YesOrNoInteger.no);
					outStockShow.setModelStatus(ModelStatus.add);
					
					if (outStockShow.getOutStockShowNum()>0) {
						outStockShows.add(outStockShow);
					}
					
				}
				HashMap<String, OutStockShow> map=new HashMap<String, OutStockShow>();
				
				for (OutStockShow outStockShow : outStockShows) {
					String id=outStockShow.getCargoId();
					if (map.containsKey(id)) {
						Double number=map.get(id).getOutStockShowNum();
						Double money=map.get(id).getShowPrice();
						
						number+=outStockShow.getOutStockShowNum();
						money+=outStockShow.getShowPrice();
						map.get(id).setOutStockShowNum(number);
						map.get(id).setShowPrice(money);
					}else {
						map.put(id, outStockShow);
						
					}
					
				}
				
				Iterator<Entry<String, OutStockShow>> iter = map.entrySet().iterator();
				while (iter.hasNext()) {
					outStockShowsForAdd.add(iter.next().getValue());
				}
				
				for (OutStockShow outStockShow : outStockShowsForAdd) {
					Stock stock=stockService.queryModelByKey(outStockShow.getCargoId());
					if( stock!= null){
						stock.setModelStatus(ModelStatus.update);
						
						stock.setStockNum(stock.getStockNum() - outStockShow.getOutStockShowNum());
						stockService.applyChange(stock);
					}
					
					List<StockCollocation> stockCollocations = new ArrayList<StockCollocation>();
					for (int j = 0; j < collocationIds.length; j++) {
						OutStockCollocation outStockCollocation=new OutStockCollocation();
						Collocation collocation=collocationService.queryModel(collocationIds[j]);
						if(outStockShow.getCargoId().equals(collocation.getCargoId())){
							if (StringUtils.isEmpty(outStockCollocation.getOutStockCollocationId())) {
								outStockCollocation.setOutStockCollocationId(CommonUtils.getKeyId());
								outStockCollocation.setCollocationId(collocationIds[j]);
								outStockCollocation.setRealPrice(Double.parseDouble(prices[j]));
								outStockCollocation.setCollocationNum(Double.parseDouble(numStrings[j]));
								outStockCollocation.setShowId(outStockShow.getShowId());
								outStockCollocation.setCreateDate(DateUtils.getCurrent());
								outStockCollocation.setShopId(outStock.getShopId());
								outStockCollocation.setDeleteMark(YesOrNoInteger.no);
								outStockCollocation.setModelStatus(ModelStatus.add);
								
								if (outStockCollocation.getCollocationNum()>0) {
									outStockCollocations.add(outStockCollocation);
								}
								
								
							}
						}
						
						
						for(StockCollocation stockCollocation:stock.getStockCollocations()) {
							if(stockCollocation.getCollocationId().equals(outStockCollocation.getCollocationId())) {
								stockCollocation.setModifyDate(DateUtils.getCurrent());
								stockCollocation.setModelStatus(ModelStatus.update);
								stockCollocation.setCollocationNum(stockCollocation.getCollocationNum()-outStockCollocation.getCollocationNum());
								stockCollocations.add(stockCollocation);
							}
						}
					}
					stockCollocationService.applyChanges(stockCollocations);
				}
				
			}

			return outStockShowService.applyChanges(outStockShowsForAdd)&&outStockCollocationService.applyChanges(outStockCollocations);
		}
		
		return false;
	}


	@Override
	public List<OutStock> queryListMPForReturn(OutStockSearch search,
			PagerModel pagerModel) {
		
		return outStockDao.queryListMPForReturn(search,pagerModel);
	}

	@Override
	public OutStock queryModelBySee(String key) {
		
		return outStockDao.queryModelBySee(key);
	}

	@Override
	public int queryCountMPForReturn(OutStockSearch search) {
		
		return outStockDao.queryCountMPForReturn(search);
	}

	@Override
	public boolean submitModifySave(Billing billing, SendOutGoodsInfo sendOutGoodsInfo,String outStockId) {
		List<OutStockCollocation> outStockCollocationsYuan =new ArrayList<OutStockCollocation>();
		List<OutStockShow> outStockShowsYuan =new ArrayList<OutStockShow>();
		if(StringUtils.isEmpty(sendOutGoodsInfo.getCollocationNum())){
			return true;
		}else {
			String[] cargoIds=sendOutGoodsInfo.getCargoId().split(",");
			String[] collocationIds=sendOutGoodsInfo.getCollocationId().split(",");
			String[] realPrices=sendOutGoodsInfo.getRealPrice().split(",");
			//String[] collocationDebts=sendOutGoodsInfo.getCollocationDebt().split(",");
			String[] collocationNums=sendOutGoodsInfo.getCollocationNum().split(",");
			
			//保存原单为为原单标记
			OutStock outStockYuan=outStockDao.queryModelById(outStockId);
			outStockYuan.setModifyMark(OutStockModifyMark.original);
			outStockYuan.setModelStatus(ModelStatus.update);
			super.applyChange(outStockYuan);
			
			OutStock outStock=new OutStock();
			outStock.setOutStockId(CommonUtils.getKeyId());
			outStock.setType(OutStockType.fahuo);
			outStock.setCreateDate(billing.getModifyDate());
			outStock.setCreateUserId(billing.getModifyUserId());
			outStock.setCreateUserName(billing.getModifyUserName());

			//申请信息
			outStock.setOutStockIdYuan(outStockId);
			outStock.setSubmitDate(billing.getModifyDate());
			outStock.setSubmitUserId(billing.getModifyUserId());
			outStock.setSubmitUserName(billing.getModifyUserName());
            outStock.setModifyMark(OutStockModifyMark.modify);
            
			outStock.setShopId(billing.getShopId());
			outStock.setOutStockState(OutStockState.yichuku);
			outStock.setBillingId(billing.getBillingId());
			outStock.setDeleteMark(YesOrNoInteger.no);
			outStock.setModelStatus(ModelStatus.add);
			
			List<OutStockShow> outStockShows=new ArrayList<OutStockShow>();
			
			List<OutStockCollocation> outStockCollocations=new ArrayList<OutStockCollocation>();
			for(int i=0;i<collocationNums.length;i++){
				if(Integer.parseInt(collocationNums[i])>0) {
					OutStockCollocation outStockCollocation=new OutStockCollocation();
					
					Double realPrice=Double.parseDouble(realPrices[i]);
					Double collocationNum=Double.parseDouble(collocationNums[i]);
					
					Boolean isCreateOutStockShow=true;
					for(OutStockShow outStockShow:outStockShows) {
						if(outStockShow.getCargoId().equals(cargoIds[i])) {
							outStockShow.setShowPrice(outStockShow.getShowPrice()+collocationNum*realPrice);
							outStockShow.setOutStockShowNum(outStockShow.getOutStockShowNum()+collocationNum);
							isCreateOutStockShow=false;
							outStockCollocation.setShowId(outStockShow.getShowId());
						}
					}
					if(isCreateOutStockShow) {
						OutStockShow outStockShow=new OutStockShow();
						outStockShow.setShowId(CommonUtils.getKeyId());
						outStockShow.setOutStockId(outStock.getOutStockId());
						outStockShow.setShowPrice(collocationNum*realPrice);
						outStockShow.setOutStockShowNum(collocationNum);
						outStockShow.setOutStockShowDebt(0.0);
						outStockShow.setReturnCargoNumShow(0.0);
						outStockShow.setDeleteMark(YesOrNoInteger.no);
						outStockShow.setCargoId(cargoIds[i]);
						outStockShow.setModelStatus(ModelStatus.add);
						outStockShows.add(outStockShow);
						outStockCollocation.setShowId(outStockShow.getShowId());
					}
					outStockCollocation.setOutStockCollocationId(CommonUtils.getKeyId());
					outStockCollocation.setCollocationId(collocationIds[i]);
					outStockCollocation.setCollocationNum(collocationNum);
					outStockCollocation.setCollocationDebt(0.0);
					outStockCollocation.setSendCargoNum(0.0);
					outStockCollocation.setReturnCargoNumCollocation(0.0);
					outStockCollocation.setRealPrice(realPrice);
					/*outStockCollocation.setComment(comments[i]);*/
					outStockCollocation.setDeleteMark(YesOrNoInteger.no);
					outStockCollocation.setModelStatus(ModelStatus.add);
					outStockCollocations.add(outStockCollocation);
					
				}
				
			}
			Double outStockNumAll=0.0;

			for(OutStockShow outStockShow:outStockShows) {
				outStockNumAll+=outStockShow.getOutStockShowNum();

			}

			outStock.setOutStockNum(outStockNumAll);
			outStock.setOutStockDebt(0.0);
			outStock.setOutStockTypeNum(outStockShows.size());
			outStock.setReturnCargoNum(0.0);
			
			outStockShowService.applyChanges(outStockShowsYuan);
			outStockCollocationService.applyChanges(outStockCollocationsYuan);
			if(super.applyChange(outStock)) {
				return outStockShowService.applyChanges(outStockShows)&&outStockCollocationService.applyChanges(outStockCollocations);
			}
		}
		return false;
	
	}

	@Override
	public boolean affirmModifySave(OutStock outStock, OutStock outStockYuan,String endType) {
		if("waive".equals(endType)) {
			outStockYuan.setModelStatus(ModelStatus.update);
			outStockYuan.setModifyMark(OutStockModifyMark.modification);
			
			outStock.setModifyMark(OutStockModifyMark.waiveModify);
			outStock.setBillingType(BillingType.close);
			outStock.setModelStatus(ModelStatus.update);
			
			if(super.applyChange(outStock)) {
				return super.applyChange(outStockYuan);
			}
		}else if("affirm".equals(endType)) {
			return modifySave(outStock, outStockYuan);
		}
		return true;
	}
	
	public boolean modifySave(OutStock outStock, OutStock outStockYuan) {
		List<OutStockCollocation> outStockCollocationsYuan =new ArrayList<OutStockCollocation>();
		Billing billingYuan=billingService.getBillingByKey(outStock.getBillingId());
		
		outStockYuan.setModelStatus(ModelStatus.update);
		outStockYuan.setBillingType(BillingType.close);
		
		
		billingYuan.getOutStock().setOutStockDebt(billingYuan.getOutStock().getOutStockDebt()-outStock.getOutStockNum()+ outStockYuan.getOutStockNum());
		if(billingYuan.getOutStock().getOutStockDebt()<=0) {
			billingYuan.setBillingState(BillingState.completed);
		}else {
			billingYuan.setBillingState(BillingState.notShipped);
		}
		billingYuan.setModelStatus(ModelStatus.update);
		billingService.applyChange(billingYuan);
		billingYuan.getOutStock().setModelStatus(ModelStatus.update);
		super.applyChange(billingYuan.getOutStock());
		
		outStock.setModifyMark(OutStockModifyMark.modification);
		outStock.setModelStatus(ModelStatus.update);
		for (OutStockShow outStockShow : outStock.getOutStockShows()) {
			
			for (OutStockShow outStockShowYuan : billingYuan.getOutStock().getOutStockShows() ) {
				if(outStockShow.getCargoId().equals(outStockShowYuan.getCargoId())) {
					outStockShowYuan.setOutStockShowDebt(outStockShowYuan.getOutStockShowDebt()-outStockShow.getOutStockShowNum());
					
				}
			}
			
			Stock stock=stockService.queryModelByKey(outStockShow.getCargoId());
			if( stock!= null){
				stock.setModelStatus(ModelStatus.update);
				
				stock.setStockNum(stock.getStockNum() - outStockShow.getOutStockShowNum());
				stockService.applyChange(stock);
			}
			List<StockCollocation> stockCollocations = new ArrayList<StockCollocation>();
			for (OutStockCollocation outStockCollocation : outStockShow.getOutStockCollocations()) {
				
				for(StockCollocation stockCollocation:stock.getStockCollocations()) {
					if(stockCollocation.getCollocationId().equals(outStockCollocation.getCollocationId())) {
						stockCollocation.setModelStatus(ModelStatus.update);
						stockCollocation.setCollocationNum(stockCollocation.getCollocationNum()-outStockCollocation.getCollocationNum());
						stockCollocations.add(stockCollocation);
					}
				}
				
				for (OutStockShow outStockShowYuan : billingYuan.getOutStock().getOutStockShows() ) {
					for (OutStockCollocation outStockCollocationYuan : outStockShowYuan.getOutStockCollocations()) {
						
						if(outStockCollocation.getCollocationId().equals(outStockCollocationYuan.getCollocationId())) {
							outStockCollocationYuan.setCollocationDebt(outStockCollocationYuan.getCollocationDebt()-outStockCollocation.getCollocationNum());
						}
					}
				}
			}
			stockCollocationService.applyChanges(stockCollocations);
		}
		//原出库信息
		for (OutStockShow outStockShow : outStockYuan.getOutStockShows()) {
			
			for (OutStockShow outStockShowYuan : billingYuan.getOutStock().getOutStockShows() ) {
				outStockShowYuan.setModelStatus(ModelStatus.update);
				if(outStockShow.getCargoId().equals(outStockShowYuan.getCargoId())) {
					outStockShowYuan.setOutStockShowDebt(outStockShowYuan.getOutStockShowDebt()+outStockShow.getOutStockShowNum());
					
				}
			}
			
			Stock stock=stockService.queryModelByKey(outStockShow.getCargoId());
			if( stock!= null){
				stock.setModelStatus(ModelStatus.update);
				
				stock.setStockNum(stock.getStockNum() + outStockShow.getOutStockShowNum());
				stockService.applyChange(stock);
			}
			List<StockCollocation> stockCollocations = new ArrayList<StockCollocation>();
			for (OutStockCollocation outStockCollocation : outStockShow.getOutStockCollocations()) {
				
				for(StockCollocation stockCollocation:stock.getStockCollocations()) {
					if(stockCollocation.getCollocationId().equals(outStockCollocation.getCollocationId())) {
						stockCollocation.setModelStatus(ModelStatus.update);
						stockCollocation.setCollocationNum(stockCollocation.getCollocationNum()+outStockCollocation.getCollocationNum());
						stockCollocations.add(stockCollocation);
					}
				}
				
				for (OutStockShow outStockShowYuan : billingYuan.getOutStock().getOutStockShows() ) {
					for (OutStockCollocation outStockCollocationYuan : outStockShowYuan.getOutStockCollocations()) {
						outStockCollocationYuan.setModelStatus(ModelStatus.update);
						if(outStockCollocation.getCollocationId().equals(outStockCollocationYuan.getCollocationId())) {
							outStockCollocationYuan.setCollocationDebt(outStockCollocationYuan.getCollocationDebt()+outStockCollocation.getCollocationNum());
						}
						outStockCollocationsYuan.add(outStockCollocationYuan);
					}
				}
			}
			stockCollocationService.applyChanges(stockCollocations);
		}
		billingRecordService.saveBillingRecord(outStock.getBillingId(),outStock.getOutStockNum(),0.0,BillingRecordType.sendCargoModify,
				outStock.getOutStockId(),outStock.getShopId(),outStock.getCreateUserId(),outStock.getCreateUserName());
		outStockCollocationService.applyChanges(outStockCollocationsYuan);
		outStockShowService.applyChanges(billingYuan.getOutStock().getOutStockShows());
		if(super.applyChange(outStock)) {
			return super.applyChange(outStockYuan);
		}
		return false;
	}

	@Override
	public boolean deleteOutStockData(String billingId) {
		
		return outStockDao.deleteOutStockData(billingId);
	}

	@Override
	public boolean deleteOutStockShowData(String billingId) {
		
		return outStockDao.deleteOutStockShowData(billingId);
	}

	@Override
	public boolean deleteOutStockCollocationData(String billingId) {
		
		return outStockDao.deleteOutStockCollocationData(billingId);
	}
}

